package com.inspur

import org.apache.spark.sql.{Dataset, SparkSession}

case class Student(name:String, age:Int)
case class Person(id:Int, name:String, age:Int, phone:String)

object SparkSQLTest0617 {

  def main(args: Array[String]): Unit = {
    val sess = SparkSession.builder()
      .appName("Test")
      .master("local")
      .getOrCreate()
//    val stu = List(Student("jack",18),Student("tom",22))
//    val df = sess.createDataFrame(stu)
//    df.show()

    val rdd = sess.sparkContext.textFile("file:///usr/local/C:/Software/idea/Work/SparkB/src/main/java/com/inspur/person.txt")
      .map(_.split(" "))
      .map(arr => Person(arr(0).toInt,arr(1),arr(2).toInt, arr(3)))

    val df = sess.createDataFrame(rdd)

//    df.select("id","name", "age", "phone")
//      .groupBy("age")
//      .count()
//      .show()

//    df.select("id").show()

//    df.filter("age > 30").show()

    df.sort("age").show()

//    df.createOrReplaceTempView("person")
//    sess.sql("select age, count(1) from person where age > 10 group by age ").show()
  }

}
